﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EBA.Reporting.Spreadsheet
{
    public class WorksheetCollection:IEnumerable<Worksheet>
    {
        List<Worksheet> Worksheets = new List<Worksheet>();

        /// <summary>
        /// 根据工作表名称获取工作表实例
        /// </summary>
        /// <param name="name">工作表名称</param>
        /// <returns>工作表</returns>
        public Worksheet this[string name]
        {
            get
            {
                return this.Worksheets.Where(i => i.Name == name).FirstOrDefault();
            }
        }

        /// <summary>
        /// 新增工作表
        /// </summary>
        /// <param name="worksheet"></param>
        internal void Add(Worksheet worksheet)
        {
            if (this.Worksheets.Any(i => i.Name == worksheet.Name))
            {
                throw new DuplicatedWorksheetNameException(worksheet.Name);
            }

            this.Worksheets.Add(worksheet);
        }

        /// <summary>
        /// 删除指定位置的工作表
        /// </summary>
        /// <param name="index">索引</param>
        internal void RemoveAt(int index)
        {
            this.Worksheets.RemoveAt(index);
        }

        /// <summary>
        /// 删除指定名称的工作表
        /// </summary>
        /// <param name="name">工作表名称</param>
        internal void RemoveNamed(string name)
        {
            var worksheet = this[name];
            if (worksheet != null)
            {
                this.Worksheets.Remove(worksheet);
            }
        }

        /// <summary>
        /// 删除指定的工作表
        /// </summary>
        /// <param name="worksheet"></param>
        internal void Remove(Worksheet worksheet)
        {
            this.Worksheets.Remove(worksheet);
        }

        /// <summary>
        /// 返回工作表集合的迭代器
        /// </summary>
        /// <returns></returns>
        public IEnumerator<Worksheet> GetEnumerator()
        {
            return this.Worksheets.GetEnumerator();
        }


        /// <summary>
        /// 返回工作表集合的迭代器
        /// </summary>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.Worksheets.GetEnumerator();
        }
    }
}
